
package mz.matafe.bolao

import grails.test.*

/**
 * Integration Test Class for a Group.
 *
 * @since 0.0.1
 * @author <a href="mailto:matafe@gmail.com">Maurício T. Ferraz</a>
 */
class GroupIntegrationTests extends GrailsUnitTestCase {

   def group

    protected void setUp() {
        super.setUp()
        group = new Group(code: 'A', name: 'Grupo A')
    }

    protected void tearDown() {
        super.tearDown()
    }

    /**
     * Testing saving
     */
    void testSave() {
        assertNotNull group.save()
        assertNotNull group.id
        def foundGroup = Group.get(group.id)
        assertEquals 'A',foundGroup.code
        assertEquals 'Grupo A',foundGroup.name
    }

    /**
     * Testing save/update
     */
    void testSaveAndUpdate() {
        assertNotNull group.save()
        assertNotNull group.id
        def foundGroup = Group.get(group.id)
        assertEquals 'A',foundGroup.code
        assertEquals 'Grupo A',foundGroup.name
        foundGroup.name = 'Grupo AA'
        foundGroup.save()
        def editGroup = Group.get(group.id)
        assertEquals 'A',editGroup.code
        assertEquals 'Grupo AA', editGroup.name

    }

    /**
     * Testing delete
     */
    void testDelete() {
        assertNotNull group.save()
        assertNotNull group.id
        def foundGroup = Group.get(group.id)
        assertEquals 'A',foundGroup.code
        assertEquals 'Grupo A',foundGroup.name
        foundGroup.delete()
        assertFalse Group.exists(foundGroup.id)
    }

    /**
     * Testing add team to a group
     */
    void testAddTeams() {
        assertNotNull group.save()
        assertNotNull group.id
        def foundGroup = Group.get(group.id)
        assertEquals 'A',foundGroup.code
        assertEquals 'Grupo A',foundGroup.name
        group.addToTeams(new Team(name: 'Brasil'))
        group.addToTeams(new Team(name: 'Africa do Sul'))
        group.addToTeams(new Team(name: 'Estados Unidos'))
        group.addToTeams(new Team(name: 'Alemanha'))
        assertEquals 4, Group.get(group.id).teams.size()
        assertEquals(['Africa do Sul','Alemanha','Brasil','Estados Unidos'],
            group.teams*.name.sort())
    }
}